<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>主页</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    <script src="./utils/flexible.js"></script>
    <link rel="stylesheet" href="./css/index.css">
</head>
<body>
    <header>
        <img src="./images/header_logo.png" alt="">
        <img src="./images/header_app.png" alt="">
    </header>
    <div class="search">
        <input type="text" placeholder="请输入你想比较的商品">
        <p>搜索</p>
    </div>
    
    <!-- 菜单栏 -->
    <ul class="ulmeun"></ul>
    
    <div class="dv">
        <p>超值折扣推荐</p>
        <p> ▶</p>
    </div>
    
    <!-- 商品栏 -->
    <ul class="ulshop"></ul>
    
    <!-- 更多优惠信息 -->
    <div class="more">
        更多优惠信息
    </div>

    <!-- 品牌排行 -->
    <div class="pinpai">
        <p>品牌排行</p>
        <p> ▶</p>
    </div>

    <!-- 底部 -->
    <footer>
        <ul>
            <li>登录</li>
            <li>注册</li>
            <li class="returntop">返回顶部</li>
        </ul>
        <p>
            手机APP下载慢慢买手机版-掌上比价平台<br>
            m.manmanbuy.com

        </p>
    </footer>

</body>
</html>
<script>
    
    // 菜单栏函数
    function render1(data=[]){
        let html="";
        data.forEach(item=>{
            html+=`
                <li indexmenuid=${item.indexmenuId} titlehref=${item.titlehref}>
                    ${item.img}
                    <p>${item.name}</p>
                </li>
                  `
        })
        $('.ulmeun').html(html)
    }
    // 商品栏函数
    function render2(data=[]){
        let html="";
        data.forEach(item=>{
            html+=`
                <li productid=${item.productId}>
                    <div>${item.productImg2}</div>
                    <p>${item.productName}
                       <span>${item.productPinkage}</span>
                    <br><br>
                        ${item.productFrom}&nbsp;
                        ${item.productTime}&nbsp;&nbsp; 
                        ${item.productComCount}    
                    </p>
                </li>
                  `
        })
        $('.ulshop').html(html)
    }
    /******************ajax请求数据*******************************/ 
    // 请求菜单数据
    $.ajax({
        url:'http://chst.vip:1234/api/getindexmenu'
    })
    .then(res=>{
        console.log(res.result);
        render1(res.result);
        let on=0;
        $('.ulmeun').on('click','li',function(){
            let indexmenuid=$(this).attr('indexmenuid');
            let titlehref=$(this).attr('titlehref')
            // 点击更多菜单栏变为三行，再点击变为两行
            if(indexmenuid==7){
                if(!on){
                    on=!on;
                    $('.ulmeun').stop().animate({
                    height:281.25
                    },300)
                }else{
                    on=!on;
                    $('.ulmeun').stop().animate({
                    height:187.5
                    },300)
                }
                
            }else{
                console.log(titlehref);
                location.href = `./html/${titlehref}?indexmenuid=${indexmenuid}`
            }
        })
    })

    // 请求商品信息数据
    $.ajax({
        url:'http://chst.vip:1234/api/getmoneyctrl'
    })
    .then(res=>{
        console.log(res.result);
        render2(res.result);
        // 跳转到详情页
        $('.ulshop').on('click','li',function(){
            let productid = $(this).attr("productid")
            location.href = `./html/shopdetail.html?productid=${productid}`
        }) 
        // 返回顶部
        $('footer .returntop').click(function(e){
            document.documentElement.scrollTop=0;
        })
    })


</script>